// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   AddInformacionAction.java

package org.fiel.admin.struts.actions;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.upload.FormFile;
import org.fiel.admin.dao.InformacionDAO;
import org.fiel.admin.struts.forms.InformacionForm;
import org.fiel.helpers.Enums;

// Referenced classes of package org.fiel.admin.struts.actions:
//            AdminAction

public final class AddInformacionAction extends AdminAction
{

    public AddInformacionAction()
    {
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
        HttpSession session = request.getSession();
        ActionErrors errors = new ActionErrors();
        InformacionForm informacionForm = (InformacionForm)form;
        InformacionDAO iDAO = new InformacionDAO();
        try
        {
            if(!isAuthorized(request))
                throw new Exception("Usuario no autorizado para esta operaci\363n.");
            writeFiles(informacionForm);
            iDAO.createInformacion(informacionForm);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
            errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError("error.exception"));
            errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError("error.detail", e.getMessage()));
        }
        if(mapping.getAttribute() != null)
            if("request".equals(mapping.getScope()))
                request.removeAttribute(mapping.getAttribute());
            else
                session.removeAttribute(mapping.getAttribute());
        if(!errors.isEmpty())
        {
            saveErrors(request, errors);
            return mapping.findForward("failed");
        } else
        {
            return mapping.findForward("success");
        }
    }

    public void writeFiles(InformacionForm informacionForm)
        throws Exception
    {
        File file = null;
        FileOutputStream fos = null;
        String path = getServlet().getServletContext().getRealPath(Enums.getPath(Integer.parseInt(informacionForm.getIdentipo())));
        try
        {
            if(informacionForm.getNameFile().getFileSize() > 0)
            {
                file = new File(path + File.separator + informacionForm.getNameFile().getFileName());
                fos = new FileOutputStream(file);
                fos.write(informacionForm.getNameFile().getFileData());
                fos.close();
            }
            if(informacionForm.getNameFileE().getFileSize() > 0)
            {
                file = new File(path + File.separator + informacionForm.getNameFileE().getFileName());
                fos = new FileOutputStream(file);
                fos.write(informacionForm.getNameFileE().getFileData());
                fos.close();
            }
            if(informacionForm.getImagen().getFileSize() > 0)
            {
                file = new File(path + File.separator + informacionForm.getImagen().getFileName());
                fos = new FileOutputStream(file);
                fos.write(informacionForm.getImagen().getFileData());
                fos.close();
            }
        }
        catch(Exception e)
        {
            throw new Exception(e);
        }
    }
}
